import {observable, action, toJS} from 'mobx';
import { Tools, Bubble } from 'weaver-mobile';
import { ListViewPage } from 'weaver-mobile-page';
import * as Api from '../../api/UserCenterApi';
const getLabel = Tools.getLabel;

export default class MsgRemindStore {
  @observable msgRemindList = new ListViewPage.Store({ route:'/userCenter/msgRemind', id:'msgRemind' });
  @observable igNoreDisabled = false;

  @action('获取列表')
  getMsgRemindList = () => {
    this.msgRemindList.setState({loading: true});
    Api.getMsgRemindList().then(data => {
      const { remindList, status } = data;
      if (remindList && remindList.length === 0) {
        this.igNoreDisabled = true;
      } else {
        const newState = {
          isTable: false,
          loading: false,
          haveCheck: true,
          template: {default: []},
          hasMore: false,
          data: [...remindList],
        };
        this.msgRemindList.setState(newState);
        this.msgRemindList.updateDataSource();
      }
    });
  }

  @action('同意、拒绝（操作）')
  dealMsgRemind = (params,callback) => {
    Api.dealMsgRemind(params).then(data => {
      if (data.status === '1') {
        Bubble.success(getLabel(30700, '操作成功')+'!', 1);
        if (callback) callback();
      } else {
        Bubble.error(getLabel('83912','操作失败！'));
      }
    })
  }

  @action('删除操作的记录')
  removeOperatedItem = (id) => {
    let tempArr = [];
    toJS(this.msgRemindList.data).map((item)=>{
      if (item.id !== id) return tempArr.push(item)
    });
    this.msgRemindList.setState({data: tempArr});
    this.msgRemindList.updateDataSource();
    if (this.msgRemindList.data.length === 0) this.igNoreDisabled = true;
  }

  @action('忽略')
  neglectMsgRemind = (params={}, callback) => {
    let tempArr = [];
    Api.neglectMsgRemind(params).then(res => {
      if (res.status === '1') {
        toJS(this.msgRemindList.data).forEach( (ele, index) => {
          if(ele.id !== params.id) {
            tempArr.push(ele)
          }
        });
        this.msgRemindList.setState({data: tempArr});
        this.msgRemindList.updateDataSource();
        Bubble.success(getLabel(30700, '操作成功')+'!', 1);
        if (this.msgRemindList.data.length === 0) this.igNoreDisabled = true;
        if (callback) callback();
      }else {
        Bubble.error(getLabel('83912','操作失败！'));
      }
    })
  }

  @action('全部忽略')
  neglectMsgRemindAll = callback => {
    Api.neglectMsgRemindAll().then(res => {
      if (res.status == '1') {
        this.msgRemindList.clearData();
        this.igNoreDisabled = true;
        Bubble.success(getLabel(30700, '操作成功')+'!', 1);
        if(callback) callback();
      }else {
        Bubble.error(getLabel('83912','操作失败！'));
      }
    })
  }

  @action('组件卸载操作')
  msgClear = () => {
    this.igNoreDisabled = false;
  }
}